/**
 * Created by Pyj on 2019/08/20.
 */
/***********************面板-start****************************/
var rowData=[];

//应用主列表参数面板
var reportGridOpt={
    id:"reportGrid",//渲染对象ID 控件的唯一标识
    autoWidth:true,
    autoHeight:true,
    className:"test",//表格样式类名
    isMulitCheck:true,//是否支持多选
    loadMsg:"正在加载数据请稍等",//表格加载显示信息
    rowData:rowData,
    root:"data",//服务端返回的数据对象名
    url:basePath+"/reporttab/list",
    columns:[
        {
            hidden : false,
            label : "报表名称",
            name : "name",
            width : "40"
        },
        {
            hidden : false,
            label : "填报周期",
            name : "cycle",
            width : "40",
            formatter : function(columnValue) {
                if(columnValue==1){
                    return "每天";
                } else if(columnValue==2) {
                    return "每周";
                } else if(columnValue==3) {
                    return "自定义";
                }
            }
        },{
            hidden : false,
            label : "频次",
            name : "frequency",
            width : "20"

        },{
            hidden : false,
            label : "有效期",
            name : "startTime",
            width : "60",
            formatter: function (columnValue,rowData) {
                return formatDateTime(columnValue)+"-"+formatDateTime(rowData.endTime)
            }
        },
        {
            hidden : false,
            label : "状态",
            name : "status",
            width : "20",
            formatter : function(columnValue) {
                if(columnValue==1){
                    return "启用";
                } else if(columnValue==0) {
                    return "禁用";
                }
            }
        },
        {
            hidden : false,// 是否隐藏该列
            label : "操作",// 显示列名称
            name : "id",// 列字段属性名`
            width : "60",// 列宽度
            isOrder : true,// 是否排序
            isFmtHtml:true,//格式化是否返回html
            formatter : function(columnValue,rowData){
                var btns=[];
                if (rowData.status == 1) {
                    var delBtn = $("<a class='griddel_btn' style='color:red;' href=\"javascript:changeStatus('"+rowData.id+"','0');\">禁用</a>");
                    btns.push(delBtn);
                } else if (rowData.status == 0){
                    var delBtn = $("<a class='griddel_btn' style='color:red;' href=\"javascript:changeStatus('"+rowData.id+"','1');\">启用</a>");
                    var editBtn = $("<a class='gridedit_btn' style='color:red;margin-left: 10px;' \">编辑</a>");
                    editBtn.bind("click", function () {
                        editReport(rowData);
                    });
                    btns.push(delBtn);
                    btns.push(editBtn);
                }
                return btns;
            }
        }
    ],
    event:{
        readyLoad:function(opt,gridObj){//grid数据加载前执行
            $(".panel").css("display","none");
        },
        restGridHeight:function(gridHeight,gridObj){
            return gridHeight-$("#searchForm").height();
        },
        onRowMouse:function (rowData,gridObj) {
            return [];
        },
    }
};

//应用添加面板参数
var addAppPanelOpt = {
    bombType:BOMB_TYPE2,
    valForm:{
        valParam:[//验证参数
            {
                inputname:"name_App_AddPanel",
                isnull:true,
                isnullmsg:"应用名称不能为空！",
                event:"submit"
            },
            {
                inputname:"cycle_App_AddPanel",
                isnull:true,
                isnullmsg:"填报周期不能为空！",
                event:"submit"
            },
            {
                inputname:"frequency_App_AddPanel",
                isnull:true,
                isnullmsg:"填报频次不能为空！",
                event:"submit"
            },
            {
                inputname:"beginDt",
                isnull:true,
                isnullmsg:"开始时间不能为空！",
                event:"submit"
            },
            {
                inputname:"endDt",
                isnull:true,
                isnullmsg:"开始时间不能为空！",
                event:"submit"
            }
        ]
    },//验证
    action:basePath+"/app/addApp",//表单提交地址
    event:{
        onClosed:function(obj){

        },
        okBtnClick:function(eventObj,panel){

        },
        submitSuccessed:function(data,panelObj){
            setTimeout(function(){
                panelObj.close();
                $("#reportGrid").wdfGrid("reload",reportGridOpt);
            },500);
        }
    }
};

//应用编辑面板参数
var editAppPanelOpt = {
    width:width,//高
    height:height,//宽
    bombType:BOMB_TYPE2,
    valForm:{
        valParam:[//验证参数
            {
                inputname:"name_App_EditPanel",
                isnull:true,
                isnullmsg:"应用名称不能为空！",
                event:"submit"
            },
            {
                inputname:"appType_App_EditPanel",
                isnull:true,
                isnullmsg:"应用类型不能为空！",
                event:"submit"
            }
        ]
    },//验证
    action:basePath+"/app/editApp",//表单提交地址
    event:{
        onClosed:function(obj){

        },
        okBtnClick:function(eventObj,panel){

        },
        submitSuccessed:function(data,panelObj){
            setTimeout(function(){
                reportGridOpt.keepPage=true;
                panelObj.close();
                $("#reportGrid").wdfGrid("reload",reportGridOpt);
            },500);
        }
    }
};

/***********************面板-end****************************/

/***********************通用下拉列表-start****************************/


/***********************通用下拉列表-end****************************/

/***********************业务操作-start****************************/
//应用添加
function addReport(){
    $(".evalutePannel").css("display","block");
    $(".evalutePannelTop_title").html("添加报表");
    $("#reporttype_id_pannel").val("");
    $("#name_report_AddPanel").val("");
    $("#frequency_report_AddPanel").val("");
    $("#beginDt").val("");
    $("#endDt").val("");
}

//应用编辑
function editReport(rowData){
    $(".evalutePannel").css("display","block");
    //加载编辑数据
    if(rowData!=undefined){
        $(".evalutePannelTop_title").html("编辑报表");
        $("#reporttype_id_pannel").val(rowData.id);
        $("#reporttype_status_pannel").val(rowData.status);
        $("#name_report_AddPanel").val(rowData.name);
        $("#cycle_report_AddPanel").val(getOptionConstant(reportTypeConstant,rowData.cycle));
        $("#frequency_report_AddPanel").val(rowData.frequency);
        $("#beginDt").val(formatDate(rowData.startTime));
        $("#endDt").val(formatDate(rowData.endTime));
    }
}

function closeScorePannel(){
    $(".evalutePannel").css("display","none");
}

function changeStatus(id,status){
    var obj=$(this);
    $.wdfProgress.progress("请稍等...",$("body"));
    $.ajax({
        url:basePath+"/reporttab/updateStatus",
        data:{
            status:status,
            id:id,
            cType:'PCWEB'
        },
        dataType:"json",
        type: "post",
        contentType:"application/x-www-form-urlencoded",
        beforeSend:null,
        success:function(data){

            $.wdfProgress.showMsg(data.msg,$("body"),true,function(eventObj){

            });
            if (data.status==1){
                $("#reportGrid").wdfGrid("reload",reportGridOpt);
            }
        },
        error:function (XMLHttpRequest, textStatus, errorThrown) {
            $.wdfProgress.showMsg("系统错误或网络延迟！",$("body"),true);
        }
    });
}

function submit(){

    if(!$("#name_report_AddPanel").val()){
        layerMsg("请输入报表名称！");
        return;
    }
    if(!$("#cycle_report_AddPanel").val().trim()){
        layerMsg("请输入报表名称！");
        return;
    }
    if(!$("#frequency_report_AddPanel").val().trim()){
        layerMsg("请输入报表频次！");
        return;
    }
    if(!$("#beginDt").val().trim()){
        layerMsg("请输入开始时间！");
        return;
    }
    if(!$("#endDt").val().trim()){
        layerMsg("请输入结束时间！");
        return;
    }

    var param;
    if($("#reporttype_id_pannel").val()){
        //编辑
        param={
            id:$("#reporttype_id_pannel").val(),
            name:$("#name_report_AddPanel").val(),
            cycle:$("#cycle_report_AddPanel option:selected").val(),
            frequency:$("#frequency_report_AddPanel").val(),
            status:$("#reporttype_status_pannel").val(),
            startTime:$("#beginDt").val(),
            endTime:$("#endDt").val()
        };
        $.post(basePath+"/reporttab/update",param,function(data){
            if (data.status == 1) {
                $("#reportGrid").wdfGrid("reload",reportGridOpt);
                closeScorePannel();
            }else {
                alert(data.msg);
            }
        });
    }else {
        //新增
        param={
            name:$("#name_report_AddPanel").val(),
            cycle:$("#cycle_report_AddPanel option:selected").val(),
            frequency:$("#frequency_report_AddPanel").val(),
            status:1,
            startTime:$("#beginDt").val(),
            endTime:$("#endDt").val()
        };
        $.post(basePath+"/reporttab/save",param,function(data){
            if (data.status == 1) {
                $("#reportGrid").wdfGrid("reload",reportGridOpt);
                closeScorePannel();
            }else {
                alert(data.msg);
            }
        });
    }


}

//应用条件查询
function search() {
    reportGridOpt.param=$("#searchForm").serialize().replace(/\+/g,"");
    $("#reportGrid").wdfGrid("reload",reportGridOpt);
}

//条件查询重置
function resetSearch() {
    $('#searchForm')[0].reset();
    search();
}

//验证消息显示
function showMsg(msg,input,obj){
    var msgBar=$("<div class='msgBar'>"+msg+"</div>");
    $(".msgBar").remove();
    obj.append(msgBar);
    msgBar.animate({"opacity":1},500);
    $(input).data("showMsg",msgBar);
    $(input).addClass("error");
};

/*判断对象值是否为空*/
function ifNULL(obj) {
    var flag = false;
    if (obj == null || obj == undefined || obj == '') {
        flag = true;
    }
    return flag;
}

/***********************业务操作-end****************************/

/***********************页面初始化-start****************************/
$(document).ready(function() {
    reportGridOpt.param=$("#searchForm").serialize();
    reportGridOpt.height = $(document).height() - $('#searchPanel').height()-20;
    $("#reportGrid").wdfGrid("init",reportGridOpt);

    addAppPanel=$("#addAppPanel").wdfPanel("FormPanel",addAppPanelOpt);
    editAppPanel=$("#editAppPanel").wdfPanel("FormPanel",editAppPanelOpt);

});
/***********************页面初始化-end****************************/